package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.component.datatable.DataTable;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceResource;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.ResourceDAO;
import com.erpanderp.wam.adm.model.service.ServiceDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ViewScoped
@ManagedBean
public class CreateServiceBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private Service service = new Service();
	private String module;
	private String assignResourceButtonLabel = CommonLookup.ASSIGN_RESOURCE_LABEL;
	private boolean assignResourcesToService;
	private Long selectedModuleId;
	private List<Resource> resources = new ArrayList<Resource>();
	private Set<Resource> assignedResourcesSet = new HashSet<Resource>();
	private List<Resource> assignResources = new ArrayList<Resource>();
	private DataTable assignedResourcesTable;
	private Resource[] selectedResources;
	private Map<String, Long> modulesMap = new HashMap<String, Long>();
	private boolean FORM_VALID;
	ServiceDAO serviceDAO = new ServiceDAO();
	ModuleDAO moduleDAO = new ModuleDAO();
	ResourceDAO resourceDAO = new ResourceDAO();

	public CreateServiceBean() {
		List<Module> modules = moduleDAO.getAllModules();
		for (Module module : modules) {
			modulesMap.put(module.getModuleName(), module.getModuleId());
		}
	}

	public void toggleAssignPanelDisplay(ActionEvent e) {

		System.out.println("In toggleAssignPanelDisplay");

		if (assignResourceButtonLabel
				.equals(CommonLookup.ASSIGN_RESOURCE_LABEL)) {
			setAssignResourceButtonLabel(CommonLookup.DO_NOT_ASSIGN_RESOURCE_LABEL);
			setAssignResourcesToService(true);
			System.out.println(selectedModuleId);
			resources = resourceDAO.getResources(selectedModuleId);
			System.out.println("Resources size: " + resources.size());
		} else {
			setAssignResourceButtonLabel(CommonLookup.ASSIGN_RESOURCE_LABEL);
			setAssignResourcesToService(false);
		}

	}

	public void assignResourceAction(ActionEvent e) {
		assignedResourcesSet.addAll( new ArrayList<Resource>(Arrays.asList(selectedResources)));
		assignResources.clear();
		assignResources.addAll(assignedResourcesSet);	
	}

	public String assignResources() {
		System.out.println("In Assign Resources");
		return "Assigned";
	}

	public void saveService(ActionEvent e) {
		System.out.println("In save Service");
		validateForm();
		if (FORM_VALID) {
			service.setCreateDate(new Date());
			service.setCreatedBy("DEVELOPER");
			service.setStatus("ACTIVE");
			service.setModule(moduleDAO.getModuleById(selectedModuleId));
			// Create Service Resources for each assigned resources...
			for (Resource resource : assignResources) {
				ServiceResource serviceResource = new ServiceResource();
				serviceResource.setCreateDate(new Date());
				serviceResource.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				serviceResource.setStatus(CommonLookup.STATUS_ACTIVE);
				serviceResource.setResource(resource);
				if(resource.isSelected()){
					serviceResource.setStartPage(CommonLookup.START_PAGE_YES);
					service.setStartPage(resource.getFileName().substring(0, resource.getFileName().indexOf(".")));
				}	
				else
					serviceResource.setStartPage(CommonLookup.START_PAGE_NO);
				service.getServiceResources().add(serviceResource);
			}

			serviceDAO.createService(service);
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, service
							.getServiceName()
							+ " service created successfully.", ""));
			service = new Service();
		} else {

		}
	}

	private void validateForm() {
		FORM_VALID = true;

		if (ValidationUtil.isEmpty(service.getServiceName(), "Service Name",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Service Name is empty");
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(service.getDisplayName(), "Display Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Display Name is empty");
			FORM_VALID = false;
		}

	}

	public void removeService(ActionEvent e){
		Resource selectedResource = (Resource) assignedResourcesTable.getRowData();
		assignedResourcesSet.remove(selectedResource);
		assignResources.remove(selectedResource);	
	}
	
	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public boolean isAssignResourcesToService() {
		return assignResourcesToService;
	}

	public void setAssignResourcesToService(boolean assignResourcesToService) {
		this.assignResourcesToService = assignResourcesToService;
	}

	public String getAssignResourceButtonLabel() {
		return assignResourceButtonLabel;
	}

	public void setAssignResourceButtonLabel(String assignResourceButtonLabel) {
		this.assignResourceButtonLabel = assignResourceButtonLabel;
	}

	public Map<String, Long> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, Long> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public ServiceDAO getServiceDAO() {
		return serviceDAO;
	}

	public void setServiceDAO(ServiceDAO serviceDAO) {
		this.serviceDAO = serviceDAO;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Long getSelectedModuleId() {
		return selectedModuleId;
	}

	public void setSelectedModuleId(Long selectedModuleId) {
		this.selectedModuleId = selectedModuleId;
	}

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	public Resource[] getSelectedResources() {
		return selectedResources;
	}

	public void setSelectedResources(Resource[] selectedResources) {
		this.selectedResources = selectedResources;
	}

	public List<Resource> getAssignResources() {
		return assignResources;
	}

	public void setAssignResources(List<Resource> assignResources) {
		this.assignResources = assignResources;
	}

	public Set<Resource> getAssignedResourcesSet() {
		return assignedResourcesSet;
	}

	public void setAssignedResourcesSet(Set<Resource> assignedResourcesSet) {
		this.assignedResourcesSet = assignedResourcesSet;
	}

	public DataTable getAssignedResourcesTable() {
		return assignedResourcesTable;
	}

	public void setAssignedResourcesTable(DataTable assignedResourcesTable) {
		this.assignedResourcesTable = assignedResourcesTable;
	}

}
